package org.mtdownload.app;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import org.mtdownload.dialogs.preferences.PreferencesDialog;
import org.mtdownload.utils.PreferencesManager;

public class MTDownloadMainFrame extends JFrame {

	private static final long serialVersionUID = 3689909587247510832L;
	
	private MTDownloadContentPane contentPane=new MTDownloadContentPane(); 

	public MTDownloadMainFrame(String title) throws HeadlessException {
		super(title);
		init();
	}

	private void init(){
		setSize(PreferencesManager.getFrameSize());
		setIconImage(new ImageIcon(MTDownloadMainFrame.class.getResource(Icons.DOWNLOAD_MANAGER)).getImage());
		addMenus();
		setContentPane(contentPane);		
	}

	private void addMenus(){
		JMenuBar bar=new JMenuBar();
		JMenu fileMenu=new JMenu("檔案(F)");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		JMenuItem fileMenuExit=new JMenuItem("exit(X)");
		fileMenuExit.setAccelerator(KeyStroke.getKeyStroke("ctrl Q"));
		fileMenuExit.setMnemonic(KeyEvent.VK_X);
		fileMenuExit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){  
				exit();
			}
		});
		fileMenu.add(fileMenuExit);
		
		JMenu configurationMenu=new JMenu("Configuration");
		configurationMenu.setMnemonic(KeyEvent.VK_C);
		JMenuItem configurationDownloadPreferences=new JMenuItem("Preferences");
		configurationDownloadPreferences.setMnemonic(KeyEvent.VK_P);
		configurationDownloadPreferences.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				preferences();
			}
		});
		configurationMenu.add(configurationDownloadPreferences);
		
		JMenu helpMenu=new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);
		JMenuItem helpAboutMenu=new JMenuItem("About");
		helpAboutMenu.setMnemonic(KeyEvent.VK_A);
		helpAboutMenu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				about();
			}
		});
		helpMenu.add(helpAboutMenu);
		
		bar.add(fileMenu);
		bar.add(configurationMenu);
		bar.add(helpMenu);
		setJMenuBar(bar);
	}
	
	private void preferences(){
		PreferencesDialog dialog=new PreferencesDialog(this,"Setup preferences",true);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);
	}
	
	private void exit(){
		if(isVisible()){
			PreferencesManager.setFrameLocation(getLocationOnScreen());
		}
		PreferencesManager.setFrameSize(getSize());
		PreferencesManager.setSplitDividerLocation(contentPane.getDividerLocation());
		//Utils.clearPreferences();
		System.exit(0);
	}
	
	private void about(){
		String message="<html>MultiThread Download Application<br>Licenced under GPL<br>Copyright 2005 Sergiu Giurgiu<br>www.mtdownload.org</html>";
		JOptionPane.showMessageDialog(this,message,"MultiThread Download",JOptionPane.INFORMATION_MESSAGE);
	}
	
	protected void processWindowEvent(WindowEvent e){
		PreferencesManager.setFrameLocation(getLocationOnScreen());
		super.processWindowEvent(e);
		if(e.getID()==WindowEvent.WINDOW_CLOSING){
			exit();
		}
	}
	
}
